def main():
    print("Hello from jlc-datasheet-mcp!")
    mcp.run(transport="stdio")
"""
FastMCP quickstart example.

cd to the `examples/snippets/clients` directory and run:
    uv run server fastmcp_quickstart stdio
"""

from mcp.server.fastmcp import FastMCP

# Create an MCP server
mcp = FastMCP("Demo")


# Add an addition tool
@mcp.tool()
def hex_to_decimal(hex_str: str) -> int:
    """将4字节的十六进制字符串转换为十进制数"""
    try:
        # 移除0x前缀（如果有的话）
        hex_str = hex_str.replace("0x", "").replace("0X", "")
        
        # 确保输入长度正确（4字节 = 8个十六进制字符）
        if len(hex_str) > 8:
            raise ValueError("输入超过4字节长度")
        
        # 将输入补齐到8个字符
        hex_str = hex_str.zfill(8)
        
        # 转换为整数
        value = int(hex_str, 16)
        
        # 处理有符号整数（如果最高位为1，则为负数）
        if value & 0x80000000:
            value = -((~value & 0xFFFFFFFF) + 1)
        
        return value
    except ValueError as e:
        raise ValueError("无效的十六进制输入")
@mcp.tool()
def decimal_to_hex(decimal_str: str) -> int:
    """将十进制数转换为4字节的十六进制字符串"""
    try:
        # 转换输入为整数
        value = int(decimal_str)
        
        # 检查范围是否在4字节整数范围内
        if value < -2147483648 or value > 2147483647:
            raise ValueError("数值超出4字节整数范围")
        
        # 对于负数，转换为对应的无符号值
        if value < 0:
            value = (abs(value) ^ 0xFFFFFFFF) + 1
        
        # 转换为8位十六进制字符串
        return format(value & 0xFFFFFFFF, '08X')
    except ValueError:
        raise ValueError("无效的十进制输入")
# Add a dynamic greeting resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
    """Get a personalized greeting"""
    return f"Hello, {name}!"


# Add a prompt
@mcp.prompt()
def greet_user(name: str, style: str = "friendly") -> str:
    """Generate a greeting prompt"""
    styles = {
        "friendly": "Please write a warm, friendly greeting",
        "formal": "Please write a formal, professional greeting",
        "casual": "Please write a casual, relaxed greeting",
    }

    return f"{styles.get(style, styles['friendly'])} for someone named {name}."

if __name__ == "__main__":
    main()
